package com.suxin.okhttp.http;

import com.alibaba.fastjson.JSON;
import com.suxin.okhttp.http.builder.AbstractHttpClient;
import com.suxin.okhttp.http.core.Constant;
import com.suxin.okhttp.http.core.HttpHeaders;
import com.suxin.okhttp.http.core.HttpMethod;
import com.suxin.okhttp.http.core.MediaType;
import com.suxin.okhttp.http.core.request.HttpRequest;
import com.suxin.okhttp.http.core.request.JsonRequestBody;
import com.suxin.okhttp.http.core.response.HttpResponse;
import junit.framework.Test;
import junit.framework.TestSuite;

import java.util.HashMap;
import java.util.Map;

/**
 * Unit test for simple App.
 */
public class AppTest {

    @org.junit.Test
    public void test() {
        DefaultHttpClientBuilder builder = new DefaultHttpClientBuilder();
        AbstractHttpClient httpClient = builder.build();
        String requestPath = "http://124.71.160.103:8090/content/indexListAdvertisement";
        HttpHeaders headers = new HttpHeaders();
        headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
        headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());


        Map<String, Object> map = new HashMap<>();
        map.put("current", 0);
        map.put("pageSize", 10);
        JsonRequestBody jsonRequestBody =
                new JsonRequestBody
                .Builder()
                        .body(JSON.toJSONString(map))
                        .build();

        HttpRequest httpRequest =
                new HttpRequest.Builder()
                        .httpMethod(HttpMethod.POST)
                        .url(requestPath)
                        .headers(headers)
                        .body(jsonRequestBody)
                        .build();

        HttpResponse<Object> httpResponse =
                httpClient.execute(httpRequest, Object.class);

        Object response = httpResponse.getServiceResponse();
        System.out.println(response);
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }
}
